home *** CD-ROM | disk | FTP | other *** search
- /* glu.c */
-
- /*
- * Mesa 3-D graphics library
- * Version: 1.2
- * Copyright (C) 1995 Brian Paul (brianp@ssec.wisc.edu)
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Library General Public
- * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Library General Public License for more details.
- *
- * You should have received a copy of the GNU Library General Public
- * License along with this library; if not, write to the Free
- * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- */
-
-
- /*
- $Id: glu.c,v 1.13 1995/06/09 21:48:05 brianp Exp $
-
- $Log: glu.c,v $
- * Revision 1.13 1995/06/09 21:48:05 brianp
- * changed version string to 1.2.1
- *
- * Revision 1.12 1995/05/24 12:55:02 brianp
- * changed gluGetString version to 1.2
- *
- * Revision 1.11 1995/05/22 16:56:20 brianp
- * Release 1.2
- *
- * Revision 1.10 1995/05/16 19:17:21 brianp
- * minor changes to allow compilation with real OpenGL headers
- *
- * Revision 1.9 1995/05/15 13:38:02 brianp
- * fixed gluLookAt() bug per Michael Pichler
- *
- * Revision 1.8 1995/04/28 16:21:29 brianp
- * added tesselation errors to gluErrorString()
- *
- * Revision 1.7 1995/04/18 15:51:21 brianp
- * fixed warnings on Suns
- *
- * Revision 1.6 1995/04/17 13:45:03 brianp
- * added gluGetString for GLU 1.1
- *
- * Revision 1.5 1995/03/16 20:37:44 brianp
- * fixed gluPickMatrix
- *
- * Revision 1.4 1995/03/06 17:34:45 brianp
- * fixed gluOrtho2D bug
- * removed unused gluProject and gluUnproject functions
- *
- * Revision 1.3 1995/03/04 19:39:18 brianp
- * version 1.1 beta
- *
- * Revision 1.2 1995/02/24 15:54:21 brianp
- * ifdef'd out gluProject, gluUnproject stubs
- *
- * Revision 1.1 1995/02/24 15:45:01 brianp
- * Initial revision
- *
- */
-
-
- #include <math.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include "gluP.h"
-
-
-
- /*
- * Miscellaneous utility functions
- */
-
-
-
- #define PI 3.14159
- #define EPS 0.00001
-
-
-
-
- void gluLookAt( GLdouble eyex, GLdouble eyey, GLdouble eyez,
- GLdouble centerx, GLdouble centery, GLdouble centerz,
- GLdouble upx, GLdouble upy, GLdouble upz )
- {
- GLdouble m[16];
- GLdouble x[3], y[3], z[3];
- GLdouble mag;
-
- /* Make rotation matrix */
-
- /* Z vector */
- z[0] = eyex - centerx;
- z[1] = eyey - centery;
- z[2] = eyez - centerz;
- mag = sqrt( z[0]*z[0] + z[1]*z[1] + z[2]*z[2] );
- if (mag) { /* mpichler, 19950515 */
- z[0] /= mag;
- z[1] /= mag;
- z[2] /= mag;
- }
-
- /* Y vector */
- y[0] = upx;
- y[1] = upy;
- y[2] = upz;
-
- /* X vector = Y cross Z */
- x[0] = y[1]*z[2] - y[2]*z[1];
- x[1] = -y[0]*z[2] + y[2]*z[0];
- x[2] = y[0]*z[1] - y[1]*z[0];
-
- /* Recompute Y = Z cross X */
- y[0] = z[1]*x[2] - z[2]*x[1];
- y[1] = -z[0]*x[2] + z[2]*x[0];
- y[2] = z[0]*x[1] - z[1]*x[0];
-
- /* mpichler, 19950515 */
- /* cross product gives area of parallelogram, which is < 1.0 for
- * non-perpendicular unit-length vectors; so normalize x, y here
- */
-
- mag = sqrt( x[0]*x[0] + x[1]*x[1] + x[2]*x[2] );
- if (mag) {
- x[0] /= mag;
- x[1] /= mag;
- x[2] /= mag;
- }
-
- mag = sqrt( y[0]*y[0] + y[1]*y[1] + y[2]*y[2] );
- if (mag) {
- y[0] /= mag;
- y[1] /= mag;
- y[2] /= mag;
- }
-
- #define M(row,col) m[col*4+row]
- M(0,0) = x[0]; M(0,1) = x[1]; M(0,2) = x[2]; M(0,3) = 0.0;
- M(1,0) = y[0]; M(1,1) = y[1]; M(1,2) = y[2]; M(1,3) = 0.0;
- M(2,0) = z[0]; M(2,1) = z[1]; M(2,2) = z[2]; M(2,3) = 0.0;
- M(3,0) = 0.0; M(3,1) = 0.0; M(3,2) = 0.0; M(3,3) = 1.0;
- #undef M
- glMultMatrixd( m );
-
- /* Translate Eye to Origin */
- glTranslated( -eyex, -eyey, -eyez );
- }
-
-
-
- void gluOrtho2D( GLdouble left, GLdouble right,
- GLdouble bottom, GLdouble top )
- {
- glOrtho( left, right, bottom, top, -1.0, 1.0 );
- }
-
-
-
- void gluPerspective( GLdouble fovy, GLdouble aspect,
- GLdouble zNear, GLdouble zFar )
- {
- GLdouble xmin, xmax, ymin, ymax;
-
- ymax = zNear * tan( fovy * PI / 360.0 );
- ymin = -ymax;
-
- xmin = ymin * aspect;
- xmax = ymax * aspect;
-
- glFrustum( xmin, xmax, ymin, ymax, zNear, zFar );
- }
-
-
-
- void gluPickMatrix( GLdouble x, GLdouble y,
- GLdouble width, GLdouble height,
- GLint viewport[4] )
- {
- GLfloat m[16];
- GLfloat sx, sy;
- GLfloat tx, ty;
-
- sx = viewport[2] / width;
- sy = viewport[3] / height;
- tx = (viewport[2] + 2.0 * (viewport[0] - x)) / width;
- ty = (viewport[3] + 2.0 * (viewport[1] - y)) / height;
-
- #define M(row,col) m[col*4+row]
- M(0,0) = sx; M(0,1) = 0.0; M(0,2) = 0.0; M(0,3) = tx;
- M(1,0) = 0.0; M(1,1) = sy; M(1,2) = 0.0; M(1,3) = ty;
- M(2,0) = 0.0; M(2,1) = 0.0; M(2,2) = 1.0; M(2,3) = 0.0;
- M(3,0) = 0.0; M(3,1) = 0.0; M(3,2) = 0.0; M(3,3) = 1.0;
- #undef M
-
- glMultMatrixf( m );
- }
-
-
-
- const GLubyte* gluErrorString( GLUenum errorCode )
- {
- static char *enum_err = "invalid enum";
- static char *value_err = "invalid value";
- static char *mem_err = "out of memory";
- static char *tess_err1 = "missing gluEndPolygon";
- static char *tess_err2 = "missing gluBeginPolygon";
- static char *tess_err3 = "misoriented contour";
- static char *tess_err4 = "vertex/edge intersection";
- static char *tess_err5 = "misoriented or self-intersecting loops";
- static char *tess_err6 = "coincident vertices";
- static char *tess_err7 = "colinear vertices";
- static char *tess_err8 = "intersecting edges";
- static char *tess_err9 = "not coplanar contours";
-
- switch (errorCode) {
- case GLU_INVALID_ENUM:
- return (GLubyte *) enum_err;
- case GLU_INVALID_VALUE:
- return (GLubyte *) value_err;
- case GLU_OUT_OF_MEMORY:
- return (GLubyte *) mem_err;
- case GLU_TESS_ERROR1:
- return (GLubyte *) tess_err1;
- case GLU_TESS_ERROR2:
- return (GLubyte *) tess_err2;
- case GLU_TESS_ERROR3:
- return (GLubyte *) tess_err3;
- case GLU_TESS_ERROR4:
- return (GLubyte *) tess_err4;
- case GLU_TESS_ERROR5:
- return (GLubyte *) tess_err5;
- case GLU_TESS_ERROR6:
- return (GLubyte *) tess_err6;
- case GLU_TESS_ERROR7:
- return (GLubyte *) tess_err7;
- case GLU_TESS_ERROR8:
- return (GLubyte *) tess_err8;
- case GLU_TESS_ERROR9:
- return (GLubyte *) tess_err9;
- default:
- return NULL;
- }
- }
-
-
-
- /*
- * New in GLU 1.1
- */
-
- const GLubyte* gluGetString( GLUenum name )
- {
- static char *extensions = "";
- static char *version = "1.2.1 Mesa";
-
- switch (name) {
- case GLU_EXTENSIONS:
- return (GLubyte *) extensions;
- case GLU_VERSION:
- return (GLubyte *) version;
- default:
- return NULL;
- }
- }
-
-